package com.jiaz.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.beans.Transient;
import java.io.Serializable;
import java.util.Map;

/**
 * 用户-地点-签到频率
 *
 * @author jiajiazi
 * @version 1.0
 * @date 2022/5/3 1:58
 */
@AllArgsConstructor
@NoArgsConstructor
public class UserCheckInFreq implements Serializable {
    private static final long serialVersionUID = 112423216777L;

    private Integer userId;
    private int[] checkInpost;
    private int[] checkInFreq;
    // 平方和（做缓存）
    private long squareSum;
    // user一对多好友集合
    private String userToUser;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public int[] getCheckInpost() {
        return checkInpost;
    }

    public void setCheckInpost(String checkInpost) {
        if (StringUtils.hasLength(checkInpost)) {
            String[] split = checkInpost.split(";");
            this.checkInpost = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                this.checkInpost[i] = Integer.parseInt(split[i]);
            }
            return;
        }
        this.checkInpost = null;
    }

    public int[] getCheckInFreq() {
        return checkInFreq;
    }

    public void setCheckInFreq(String checkInFreq) {
        if (StringUtils.hasLength(checkInFreq)) {
            String[] split = checkInFreq.split(";");
            this.checkInFreq = new int[split.length];
            long l = 0;
            for (int i = 0; i < split.length; i++) {
                this.checkInFreq[i] = Integer.parseInt(split[i]);
                l += Math.pow(this.checkInFreq[i], 2);
            }
            this.setSquareSum(l);
            return;
        }
        this.checkInFreq = null;
    }

    public long getSquareSum() {
        return squareSum;
    }

    public void setSquareSum(long squareSum) {
        this.squareSum = squareSum;
    }

    public String getUserToUser() {
        return userToUser;
    }

    public void setUserToUser(String userToUser) {
        this.userToUser = userToUser;
    }

    public boolean include(int venue) {
        int beg = 0, end = checkInpost.length-1;
        while (beg <= end) {
            int mid = (beg + end) / 2;
            if (venue == checkInpost[mid]) {
                return true;
            } else if (venue < checkInpost[mid]) {
                end = mid - 1;
            } else {
                beg = mid + 1;
            }
        }
        return false;
    }
}
